package com.foodost.idiomer;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.youmi.android.banner.AdSize;
import net.youmi.android.banner.AdView;
import net.youmi.android.banner.AdViewListener;
import net.youmi.android.spot.SpotManager;

import org.json.JSONException;
import org.json.JSONObject;

import com.iflytek.cloud.speech.SpeechConstant;
import com.iflytek.cloud.speech.SpeechError;
import com.iflytek.cloud.speech.SpeechListener;
import com.iflytek.cloud.speech.SpeechSynthesizer;
import com.iflytek.cloud.speech.SpeechUser;
import com.iflytek.cloud.speech.SynthesizerListener;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ExplanActivity extends Activity implements OnClickListener ,SynthesizerListener{
    private TextView explanTitle,explanContent;
    private IdiomModel mIdiomModel;
    private String explan;
    private ImageView speaker;
    private SpeechSynthesizer speechSynthesizer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.explan);
        String idiomRight = getIntent().getCharSequenceExtra(Constants.IDIOM_RIGHT).toString();
        mIdiomModel = new IdiomModel(this);
        initView(idiomRight);
        initAds();
        SpeechUser.getUser().login(ExplanActivity.this, null, null, "appid=77199457", listener);
        setParam();
    }
    
    private void initView(String idiomRight){
        explanTitle = (TextView)findViewById(R.id.explan_title);
        explanContent = (TextView)findViewById(R.id.explan_content);
        speaker = (ImageView)findViewById(R.id.speaker);
        speaker.setOnClickListener(this);
        explanTitle.setText(idiomRight);
        explan = mIdiomModel.getExplan(this,idiomRight);
        if(explan != null){
        	explanContent.setText(explan);
        }else{
        	explanContent.setText(R.string.explan_default);
        }
        AdView adView = new AdView(this,AdSize.FIT_SCREEN);
        LinearLayout adLayout = (LinearLayout)findViewById(R.id.adLayout);
        adLayout.addView(adView);
        adView.setAdListener(adViewLsn);
    }
    
    public void setParam()
    {
        speechSynthesizer = SpeechSynthesizer.createSynthesizer(this);
        speechSynthesizer.setParameter(SpeechConstant.VOICE_NAME, "nicholas");
        speechSynthesizer.setParameter(SpeechConstant.SPEED, "50");
        speechSynthesizer.setParameter(SpeechConstant.VOLUME, "50");
        speechSynthesizer.setParameter(SpeechConstant.PITCH, "50");
    }
    
    //do not use
    public void getYoudaoExplan(String idiom){
        FinalHttp fh = new FinalHttp();
        String url = "http://dict.youdao.com/search?q="+idiom+"&keyfrom=dict.index"; 
        fh.get(url, new AjaxCallBack<String>(){
            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
            }
            
            @Override
            public void onStart() {
                super.onStart();
            }
            
            @Override
            public void onSuccess(String t) {
                super.onSuccess(t);
                explan =  t;
                Log.d("nicholas", t);
                String json = t.substring(5, t.length() - 2);
                Log.d("nicholas", json);
                JSONObject jsonO = null;
                try {
                    jsonO = new JSONObject(json);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                getJsonPinYin(jsonO);
            }
        });
        
    }
    
    //do not use
    public void getJsonPinYin(JSONObject jsonO){
        Log.d("nicholas", "jsonO = " + (jsonO == null));
        if(jsonO != null){
            try {
                JSONObject jsbasic = jsonO.getJSONObject("basic");
                String pinyin = jsbasic.getString("phonetic");
                Log.d("nicholas", "pinyin = " + pinyin);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    
    private AdViewListener adViewLsn = new AdViewListener() {
        
        @Override
        public void onSwitchedAd(AdView arg0) {
            // TODO Auto-generated method stub
            
        }
        
        @Override
        public void onReceivedAd(AdView arg0) {
            // TODO Auto-generated method stub
            
        }
        
        @Override
        public void onFailedToReceivedAd(AdView arg0) {
            // TODO Auto-generated method stub
            
        }
    };
    
    public void initAds(){
        SpotManager.getInstance(this).loadSpotAds();
    }
    
    @Override
    protected void onDestroy() {
        SpotManager.getInstance(this)
        .unregisterSceenReceiver();
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.speaker){
            String title = explanTitle.getText().toString();
            String explan = explanContent.getText().toString();
            speechSynthesizer.startSpeaking(title + ": " + explan, this);
        }
    }
    
    private SpeechListener listener = new SpeechListener(){

        @Override
        public void onCompleted(SpeechError arg0) {
            
        }

        @Override
        public void onData(byte[] arg0) {
            
        }

        @Override
        public void onEvent(int arg0, Bundle arg1) {
            
        }
        
    };
    @Override
    public void onBufferProgress(int arg0, int arg1, int arg2, String arg3) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void onCompleted(SpeechError arg0) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void onSpeakBegin() {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void onSpeakPaused() {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void onSpeakProgress(int arg0, int arg1, int arg2) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void onSpeakResumed() {
        // TODO Auto-generated method stub
        
    }
    
    
    @Override
    protected void onPause() {
        super.onPause();
        speechSynthesizer.stopSpeaking();
    }
    

}
